<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <title>Vue计算属性/侦听器/方法比较</title>
    <script src="js/vue.min.js"></script>
</head>
<body>
<div id="app">
    <h1>计算属性：computed</h1>
    {{fullName}}
    <h1>方法：methods</h1>
    {{fullName2()}}
    <h1>侦听器：watch</h1>
    {{watchFullName}}
    <h1>年龄</h1>
    {{age}}
</div>
<script>
    var other = 'This is other';
    var app = new Vue({
        el:"#app",
        data:{
            firstName:"zhang",
            lastName:"san",
            watchFullName:"zhangsan",
            age:18,
        },
        watch: {
            firstName:function(newFirstName, oldFirstName){
                console.log("firstName触发了watch,newFirstName="+newFirstName+",oldFirstName="+oldFirstName)
                this.watchFullName = this.firstName+this.lastName+","+other
            },
            lastName:function(newLastName, oldLastName){
                console.log("lastName触发了watch,newLastName="+newLastName+",oldLastName="+oldLastName)
                this.watchFullName = this.firstName+this.lastName+","+other
            }
        },
        computed: {
            fullName:function(){
                console.log("调用了fullName,计算了一次属性")
                return this.firstName+this.lastName+","+other;
            }
        },
        methods: {
            fullName2:function(){
                console.log("调用了fullName,执行了一次方法")
                fullName2 = this.firstName+this.lastName+","+other;
                return fullName2;
            }
        }
    });
</script>
</body>
</html>